﻿
using Castle.Core.Internal;
using Coldairarrow.Business.Interface;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.Interface;
using Coldairarrow.Util;
using Newtonsoft.Json.Linq;
using OfficeOpenXml.Drawing.Slicer.Style;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.Tracing;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using static AutoMapper.Internal.CollectionMapperExpressionFactory;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace Coldairarrow.Business.Interface
{
    public class OL_Rep_RussianEcommerceSalesInterfaceBusiness : Interface_BaseBusiness, IJob_Business, ITransientDependency
    {
        public List<OL_FilterDTO> Filters;
        public OL_Rep_RussianEcommerceSalesInterfaceBusiness()
        {
            this.InterfaceName = "俄罗斯电商销售报表";
            //获取字段的映射关系
            tableField = GetMapField("editRow");
            //索引字段
            Keys = GetKeys("editRow");
            indexFields.Add(Keys.Where(X => X.Type == "K").First().Field);
            //查询过滤条件
            Filters = new List<OL_FilterDTO>();


            //查询执行前的事件
            BeforeQueryEvent += InterfaceBusiness_BeforeQueryEvent;
            //查询执行中的事件
            OnQueryEvent += InterfaceBusiness_OnQueryEvent;
            //执行非查询请求中的事件
            OnNoQueryEvent += InterfaceBusiness_OnNoQueryEvent;

        }

        /// <summary>
        /// 执行非查询请求中时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="noQueryEventArgs"></param>
        private void InterfaceBusiness_OnNoQueryEvent(object sender, NoQueryEventArgs noQueryEventArgs)
        {
            //API请求是否成功
            noQueryEventArgs.Success = noQueryEventArgs.Result["success"].ToString().ToBool();
        }

        /// <summary>
        /// 查询执行前的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="queryEventArgs"></param>
        private void InterfaceBusiness_BeforeQueryEvent(object sender, QueryEventArgs queryEventArgs)
        {
            //执行查询前添加过滤条件
            if (queryEventArgs.Page.Filters.IsNullOrEmpty() && !Filters.IsNullOrEmpty())
            {
                queryEventArgs.Page.Filters = Filters;
            }

        }

        /// <summary>
        /// 查询执行中的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="queryEventArgs"></param>
        private void InterfaceBusiness_OnQueryEvent(object sender, QueryEventArgs queryEventArgs)
        {
            //是否查询完成
            if (queryEventArgs.Result["data"]["rows"].ToList().Count == 0)
            {
                queryEventArgs.Page.Complete = true;
            }
            //向总行中添加新的行
            queryEventArgs.Rows.AddRange(queryEventArgs.Result["data"]["rows"].ToList());
            //获取总页数
            queryEventArgs.Page.Total = queryEventArgs.Result["data"]["total"].ToString().ToInt();
            //获取查询成功标志
            queryEventArgs.Success = queryEventArgs.Result["success"].ToString().ToBool();
        }


        /// <summary>
        /// 执行增删改查业务
        /// </summary>
        public async void ExecuteInterface()
        {

            try
            {
                //数据库连接
                ConString = JsonSettingHelper.GetSetting("Database:BaseDb:ConnectionString");
                SqlDb = (SqlServerHelper)DbHelperFactory.GetDbHelper(EFCore.Sharding.DatabaseType.SqlServer, ConString);

                //获取此接口的所有API信息
                this.interfaceOption = SqlDb.GetListBySql<Base_InterfaceOption>(@$"SELECT * FROM  Base_InterfaceOption(NOLOCK) WHERE Name='{InterfaceName}' ");
                Base_InterfaceOption queryOption = this.interfaceOption.Where(o => o.OperateType == "getFilterRows").First();//查询API配置
                Base_InterfaceOption insertOption = this.interfaceOption.Where(o => o.OperateType == "addRow").First();//新增API配置
                Base_InterfaceOption upDateOption = this.interfaceOption.Where(o => o.OperateType == "editRow").First();//更行API配置
                Base_InterfaceOption deleteOption = this.interfaceOption.Where(o => o.OperateType == "deleteRow").First();//删除API配置



                string firstDate = DateTime.Now.AddYears(-queryOption.ExcInterval).Year + "-01-01";

                //查询汇率
                BD_RateInterfaceBusiness bD_RateInterfaceBusiness = new BD_RateInterfaceBusiness();
                bD_RateInterfaceBusiness.Filters.Clear();
                bD_RateInterfaceBusiness.Filters.Add(
               new OL_FilterDTO() { ControlId = "66c5b4610604789daa40eef9", DataType = 15, SpliceType = 1, FilterType = 14, Value = firstDate }//日期大于等于最早日期的
           );
                bD_RateInterfaceBusiness.Filters.Add(
               new OL_FilterDTO() { ControlId = "66f54d9fb463a25c63567a25", DataType = 2, SpliceType = 1, FilterType = 2, Value = "RUB" }//币别为RUB的
           );
                bD_RateInterfaceBusiness.Filters.Add(
               new OL_FilterDTO() { ControlId = "683d14cea31d84c7f6537bf2", DataType = 2, SpliceType = 1, FilterType = 2, Value = "手工创建" }//创建类型为手动创建
           );


                List<View_Rate> rate = await bD_RateInterfaceBusiness.QueryAsync<View_Rate>(bD_RateInterfaceBusiness.queryOption, bD_RateInterfaceBusiness.tableField, bD_RateInterfaceBusiness.SqlDb);




                List<OL_Rep_RussianEcommerceSales> saleskLsit = new List<OL_Rep_RussianEcommerceSales>();

                List<OL_Rep_RussianEcommerceSales> sales = new List<OL_Rep_RussianEcommerceSales>();
                //获取YD销售数据
                sales = GetYDSales(firstDate.ToDateTime().Date, rate);
                saleskLsit.AddRange(sales);
                //获取WB销售数据
                sales = GetWBSales(firstDate.ToDateTime().Date, rate);
                saleskLsit.AddRange(sales);
                //获取OZON销售数据
                sales = GetOZSales(firstDate.ToDateTime().Date, rate);
                saleskLsit.AddRange(sales);
                List<OL_Rep_RussianEcommerceSales> sourceData = new List<OL_Rep_RussianEcommerceSales>();
                sourceData = saleskLsit;


                //获取目标数据
                Filters.Clear();
                Filters.Add(
                //13:>,14>=15:<,16<=
                new OL_FilterDTO() { ControlId = "", DataType = 15, SpliceType = 1, FilterType = 14, Value = "" }//FDATE字段过滤日开
            );
                List<OL_Rep_RussianEcommerceSales> targetData = await QueryAsync<OL_Rep_RussianEcommerceSales>(queryOption, tableField, SqlDb);
                var ww = targetData.GroupBy(x => x.ID).Where(x => x.Count() > 1);
                if (this.State > 0 && sourceData.Count > 0)
                {
                    //更新
                    #region
                    List<OL_Rep_RussianEcommerceSales> updataItem = sourceData.Intersect(targetData, new ComparerFromKeys<OL_Rep_RussianEcommerceSales>(Keys, "RowId")).ToList();//获取交集(更新项)
                    List<OL_RowDTO> updataData = ConvertOL_Row(updataItem, tableField);//需要更新的数据集合
                    ExecuteNoQuery<OL_RowDTO>(updataData, upDateOption, SqlDb);//执行更新
                    #endregion

                    //新增
                    #region
                    List<OL_Rep_RussianEcommerceSales> insertItem = sourceData.Except(targetData, new ComparerFromKey<OL_Rep_RussianEcommerceSales>(indexFields.First(), "RowId")).ToList();//获取源数据对目标数据的差集(新增项)
                    List<OL_RowDTO> insertData = ConvertOL_Row(insertItem, tableField);//需要新增的数据集合
                    ExecuteNoQuery<OL_RowDTO>(insertData, insertOption, SqlDb);//执行插入行
                    #endregion

                    //删除
                    #region
                    List<OL_Rep_RussianEcommerceSales> deleteItem = targetData.Except(sourceData, new ComparerFromKey<OL_Rep_RussianEcommerceSales>(indexFields.First(), "RowId")).ToList();//获取目标数对据源数据的差集(删除项)
                    List<OL_Rep_RussianEcommerceSales> repeatItem = targetData.GroupBy(x => x.GetPropertyValue(indexFields.First())).Where(x => x.Count() > 1).Select(x => x.FirstOrDefault()).ToList();
                    //将重复的项也一起加入删除的列表
                    deleteItem.AddRange(repeatItem);
                    //筛选出删除列表中重复的项
                    var temp = deleteItem.GroupBy(x => x.GetPropertyValue(indexFields.First())).Where(x => x.Count() > 1).Select(x => x.FirstOrDefault()).ToList();
                    deleteItem.AddRange(temp);
                    List<OL_RowDTO> deleteData = ConvertOL_Row(deleteItem, tableField);//需要删除的数据集合
                    ExecuteNoQuery<OL_RowDTO>(deleteData, deleteOption, SqlDb);//执行删除行
                    #endregion
                }
            }


            catch (Exception ex)
            {
                AddLog(SqlDb, ex.Message, InterfaceName, "erro", "");
            }
        }




        /// <summary>
        /// 将源数据根据字典映射关系转换成OL_RowDTO
        /// </summary>
        /// <typeparam name="T">源数据</typeparam>
        /// <param name="sourceData">源数据</param>
        /// <param name="map">Key为目标字段名称，Value为源字段名称</param>
        /// <returns></returns>
        public List<OL_RowDTO> ConvertOL_Row<T>(List<T> sourceData, Dictionary<string, string> map)
        {
            List<OL_RowDTO> list = new List<OL_RowDTO>();
            object rowId;
            foreach (var item in sourceData)
            {
                rowId = item.GetPropertyValue("RowId");
                //需要新增的数据
                OL_RowDTO row = new OL_RowDTO() { RowId = rowId.IsNullOrEmpty() ? null : rowId.ToString(), Controls = new List<Control>() };
                foreach (var c in map)
                {
                    //获取字段的键值做判断
                    var field = Keys.Where(x => x.TField == c.Key).ToList();
                    string isKey = "";
                    if (field.Count > 0) isKey = field.First().Type;
                    //判断对象的字段是否存在
                    if (item.ContainsProperty(c.Value) && isKey != "N")
                    {
                        //存在则添加数据
                        row.Controls.Add(new Control() { ControlId = c.Key, Value = item.GetPropertyValue(c.Value) });
                    }
                }
                list.Add(row);
            }
            return list;
        }


        public List<OL_Rep_RussianEcommerceSales> GetOZSales(DateTime firstDate, List<View_Rate> rateList)
        {
            List<OL_Rep_RussianEcommerceSales> dataList = new List<OL_Rep_RussianEcommerceSales>();
            #region 查询数据源
            this.SqlDb._conStrings[0] = JsonSettingHelper.GetSetting("Database:BaseDb:ConnectionString");
            //获取年第一个月第一天的日期
            //var firstDateForYear = firstDate.Year + "-01-01";

            //发货报告明细
            List<ReportPostingsCreate> orderList_All = this.SqlDb.GetListBySql<ReportPostingsCreate>(@$"SELECT * FROM SD_OZ_ReportPostingsCreate WHERE AcceptedForProcessing>='{firstDate}'");
            //退货明细
            List<FBOFBSReturns> returnList_All = this.SqlDb.GetListBySql<FBOFBSReturns>(@$"SELECT * FROM SD_OZ_FBOFBSReturns a 
LEFT JOIN SD_OZ_ReportPostingsCreate  b ON a.posting_number=b.ShipmentNumber
WHERE b.Status!='Canceled' AND return_date>='{firstDate}' ");
            List<string> selectList = new List<string>();
            //获取当日销量的产品，日期，店铺项
            var salesList = orderList_All.Where(x => !x.AcceptedForProcessing.IsNullOrEmpty() && x.AcceptedForProcessing.ToDateTime().Date >= firstDate).Select(x => x.AcceptedForProcessing.ToDateTime().ToString("yyyy-MM-dd") + x.SellerId + x.ArticleCode);
            selectList.AddRange(salesList);

            //获取当日发货的产品，日期，店铺项
            var shipmentsList = orderList_All.Where(x => !x.ActualDateOfHandingOverToDelivery.IsNullOrEmpty() && x.ActualDateOfHandingOverToDelivery.ToDateTime().Date >= firstDate).Select(x => x.ActualDateOfHandingOverToDelivery.ToDateTime().ToString("yyyy-MM-dd") + x.SellerId + x.ArticleCode);
            selectList.AddRange(shipmentsList);
            //获取当日退货的产品，日期，店铺项
            var returnsList = returnList_All.Where(x => !x.return_date.IsNullOrEmpty() && x.return_date.ToDateTime().Date >= firstDate).Select(x => x.return_date.ToDateTime().ToString("yyyy-MM-dd") + x.company_id + x.offer_id);
            selectList.AddRange(returnsList);
            //根据产品，日期，店铺分组，统计日，月，年的销量,发货，退货以及金额信息
            foreach (var item1 in selectList.Distinct())
            {

                OL_Rep_RussianEcommerceSales data = new OL_Rep_RussianEcommerceSales();
                //string month = "";
                //string year = "";
                string date = "";
                string MSKU = "";
                string seller_Id = "";
                string prdType = "";
                string brand = "";

                //获取产品当日的销售单,状态不为取消，店铺相同,产品编号相同，时间相同
                var salesTheDate = orderList_All.Where(x => !x.AcceptedForProcessing.IsNullOrEmpty() && x.AcceptedForProcessing.ToDateTime().ToString("yyyy-MM-dd") + x.SellerId + x.ArticleCode == item1);
                var salesFirst = !salesTheDate.IsNullOrEmpty() && salesTheDate.Count() > 0 ? salesTheDate.First() : null;
                if (MSKU.IsNullOrEmpty() && !salesFirst.IsNullOrEmpty())
                {
                    MSKU = salesFirst.ArticleCode;
                    seller_Id = salesFirst.SellerId;
                    prdType = salesFirst.PrdType;
                    brand = salesFirst.Brand;
                    date = salesFirst.AcceptedForProcessing.ToDateTime().ToString("yyyy-MM-dd");
                }
                //日销量
                data.SalesOnThatDay = !salesTheDate.IsNullOrEmpty() && salesTheDate.Count() > 0 ? salesTheDate.Sum(x => x.Quantity.ToInt()) : 0;
                //日销售额RUM
                data.SalesAmountOnThatDay_RUB = !salesTheDate.IsNullOrEmpty() && salesTheDate.Count() > 0 ? (decimal)salesTheDate.Sum(x => x.ShipmentAmount.ToDouble()) : 0;


                //获取产品当日的销售单,状态为取消，店铺相同,产品编号相同，时间相同
                var canceledTheDate = orderList_All.Where(x => !x.AcceptedForProcessing.IsNullOrEmpty() && x.Status == "Canceled" && x.AcceptedForProcessing.ToDateTime().ToString("yyyy-MM-dd") + x.SellerId + x.ArticleCode == item1);
                var canceledFirst = !canceledTheDate.IsNullOrEmpty() && canceledTheDate.Count() > 0 ? canceledTheDate.First() : null;
                if (MSKU.IsNullOrEmpty() && !canceledFirst.IsNullOrEmpty())
                {
                    MSKU = canceledFirst.ArticleCode;
                    seller_Id = canceledFirst.SellerId;
                    prdType = canceledFirst.PrdType;
                    brand = canceledFirst.Brand;
                    date = canceledFirst.AcceptedForProcessing.ToDateTime().ToString("yyyy-MM-dd");
                }
                //取消量
                data.CancelOnThatDay = !canceledTheDate.IsNullOrEmpty() && canceledTheDate.Count() > 0 ? canceledTheDate.Sum(x => x.Quantity.ToInt()) : 0;
                //取消售额RUM
                data.CancelAmountOnThatDay_RUB = !canceledTheDate.IsNullOrEmpty() && canceledTheDate.Count() > 0 ? (decimal)canceledTheDate.Sum(x => x.ShipmentAmount.ToDouble()) : 0;



                //获取产品当日的发货单,状态不为取消，店铺相同,产品编号相同，时间相同
                var shipmentsTheDate = orderList_All.Where(x => !x.ActualDateOfHandingOverToDelivery.IsNullOrEmpty() && x.Status != "Canceled" && x.ActualDateOfHandingOverToDelivery.ToDateTime().ToString("yyyy-MM-dd") + x.SellerId + x.ArticleCode == item1);
                var shipmentsFirst = !shipmentsTheDate.IsNullOrEmpty() && shipmentsTheDate.Count() > 0 ? shipmentsTheDate.First() : null;
                if (MSKU.IsNullOrEmpty() && !shipmentsFirst.IsNullOrEmpty())
                {
                    MSKU = shipmentsFirst.ArticleCode;
                    seller_Id = shipmentsFirst.SellerId;
                    prdType = shipmentsFirst.PrdType;
                    brand = shipmentsFirst.Brand;
                    date = shipmentsFirst.ActualDateOfHandingOverToDelivery.ToDateTime().ToString("yyyy-MM-dd");
                }
                //日发货
                data.ShipmentsOnThatDay = !shipmentsTheDate.IsNullOrEmpty() && shipmentsTheDate.Count() > 0 ? shipmentsTheDate.Sum(x => x.Quantity.ToInt()) : 0;
                //日发货额RUM
                data.ShipmentsAmountOnThatDay_RUB = !shipmentsTheDate.IsNullOrEmpty() && shipmentsTheDate.Count() > 0 ? (decimal)shipmentsTheDate.Sum(x => x.ShipmentAmount.ToDouble()) : 0;



                //获取产品当日的退货单，店铺相同,产品编号相同，时间相同
                if (item1 == "2025-04-202513164TCH29DB")
                {
                    var SS = "";

                }
                var returnsTheDate = returnList_All.Where(x => !x.return_date.IsNullOrEmpty() && x.return_date.ToDateTime().ToString("yyyy-MM-dd") + x.company_id + x.offer_id == item1);
                var returnsFirst = !returnsTheDate.IsNullOrEmpty() && returnsTheDate.Count() > 0 ? returnsTheDate.First() : null;
                if (MSKU.IsNullOrEmpty() && !returnsFirst.IsNullOrEmpty())
                {
                    MSKU = returnsFirst.offer_id;
                    seller_Id = returnsFirst.company_id;
                    var order = orderList_All.Where(x => x.ArticleCode == returnsFirst.offer_id);
                    prdType = !order.IsNullOrEmpty() && order.Count() > 0 ? order.First().PrdType : "";
                    brand = !order.IsNullOrEmpty() && order.Count() > 0 ? order.First().Brand : "";
                    date = returnsFirst.return_date.ToDateTime().ToString("yyyy-MM-dd");
                }
                //日退货
                data.ReturnsOnThatDay = !returnsTheDate.IsNullOrEmpty() && returnsTheDate.Count() > 0 ? returnsTheDate.Sum(x => x.quantity.ToInt()) : 0;
                //日退货额RUM
                data.ReturnsAmountOnThatDay_RUB = !returnsTheDate.IsNullOrEmpty() && returnsTheDate.Count() > 0 ? (decimal)returnsTheDate.Sum(x => x.price.ToDouble()) : 0;



                if (!date.IsNullOrEmpty() && !MSKU.IsNullOrEmpty())
                {
                    //获取汇率
                    var rate_ = rateList.Where(x => x.生效时间.ToDateTime().Date == date.ToDateTime().Date);
                    var rate = rate_.IsNullOrEmpty() ? 0 : rate_.First().汇率.ToDouble();
                    //日销售额RMB
                    data.SalesAmountOnThatDay_RMB = rate == 0 || data.SalesAmountOnThatDay_RUB == 0 ? 0 : Math.Round(data.SalesAmountOnThatDay_RUB / (decimal)rate, 2);
                    //日发货额RMB
                    data.ShipmentsAmountOnThatDay_RMB = rate == 0 || data.ShipmentsAmountOnThatDay_RUB == 0 ? 0 : Math.Round(data.ShipmentsAmountOnThatDay_RUB / (decimal)rate, 2);
                    //日退货额RMB
                    data.ReturnsAmountOnThatDay_RMB = rate == 0 || data.ReturnsAmountOnThatDay_RUB == 0 ? 0 : Math.Round(data.ReturnsAmountOnThatDay_RUB / (decimal)rate, 2);
                    //日取消额RMB
                    data.CancelAmountOnThatDay_RMB = rate == 0 || data.CancelAmountOnThatDay_RUB == 0 ? 0 : Math.Round(data.CancelAmountOnThatDay_RUB / (decimal)rate, 2);
                    data.Date = date;
                    data.MSKU = MSKU;
                    data.PrdType = prdType;
                    data.Brand = brand;
                    data.Seller_Id = seller_Id;
                    data.ID = data.MSKU + data.Date + data.Seller_Id;
                    dataList.Add(data);
                }
            }
            if (!dataList.IsNullOrEmpty() && dataList.Count > 0)
            {
                //计算日的金额及数量
                //dataList.GroupBy(x => x.Date.ToDateTime().ToString("yyyy-MM-dd") + x.MSKU + x.Seller_Id).ForEach(x =>
                //{

                //    var salesOnThatDay = x.Sum(x => x.SalesOnThatDay);
                //    var salesAmountOnThatDay_RMB = x.Sum(x => x.SalesAmountOnThatDay_RMB);
                //    var salesAmountOnThatDay_RUB = x.Sum(x => x.SalesAmountOnThatDay_RUB);
                //    var shipmentsOnThatDay = x.Sum(x => x.ShipmentsOnThatDay);
                //    var shipmentsAmountOnThatDay_RMB = x.Sum(x => x.ShipmentsAmountOnThatDay_RMB);
                //    var shipmentsAmountOnThatDay_RUB = x.Sum(x => x.ShipmentsAmountOnThatDay_RUB);
                //    var returnsOnThatDay = x.Sum(x => x.ReturnsOnThatDay);
                //    var returnsAmountOnThatDay_RMB = x.Sum(x => x.ReturnsAmountOnThatDay_RMB);
                //    var returnsAmountOnThatDay_RUB = x.Sum(x => x.ReturnsAmountOnThatDay_RUB);

                //    foreach (var item1 in x)
                //    {
                //        item1.SalesOnThatDay = salesOnThatDay;
                //        item1.SalesAmountOnThatDay_RMB = salesAmountOnThatDay_RMB;
                //        item1.SalesAmountOnThatDay_RUB = salesAmountOnThatDay_RUB;
                //        item1.ShipmentsOnThatDay = shipmentsOnThatDay;
                //        item1.ShipmentsAmountOnThatDay_RMB = shipmentsAmountOnThatDay_RMB;
                //        item1.ShipmentsAmountOnThatDay_RUB = shipmentsAmountOnThatDay_RUB;
                //        item1.ReturnsOnThatDay = returnsOnThatDay;
                //        item1.ReturnsAmountOnThatDay_RMB = returnsAmountOnThatDay_RMB;
                //        item1.ReturnsAmountOnThatDay_RUB = returnsAmountOnThatDay_RUB;
                //    }
                //});

                //计算月的金额及数量
                dataList.GroupBy(x => x.Date.ToDateTime().ToString("yyyy-MM") + x.MSKU + x.Seller_Id).ForEach(x =>
                {

                    var salesOnThatMonth = x.Sum(x => x.SalesOnThatDay);
                    var salesAmountOnThatMonth_RMB = x.Sum(x => x.SalesAmountOnThatDay_RMB);
                    var salesAmountOnThatMonth_RUB = x.Sum(x => x.SalesAmountOnThatDay_RUB);
                    var shipmentsOnThatMonth = x.Sum(x => x.ShipmentsOnThatDay);
                    var shipmentsAmountOnThatMonth_RMB = x.Sum(x => x.ShipmentsAmountOnThatDay_RMB);
                    var shipmentsAmountOnThatMonth_RUB = x.Sum(x => x.ShipmentsAmountOnThatDay_RUB);
                    var returnsOnThatMonth = x.Sum(x => x.ReturnsOnThatDay);
                    var returnsAmountOnThatMonth_RMB = x.Sum(x => x.ReturnsAmountOnThatDay_RMB);
                    var returnsAmountOnThatMonth_RUB = x.Sum(x => x.ReturnsAmountOnThatDay_RUB);
                    var cancelOnThatMonth = x.Sum(x => x.CancelOnThatDay);
                    var cancelAmountOnThatMonth_RMB = x.Sum(x => x.CancelAmountOnThatDay_RMB);
                    var cancelAmountOnThatMonth_RUB = x.Sum(x => x.CancelAmountOnThatDay_RUB);
                    foreach (var item1 in x)
                    {
                        item1.SalesOnThatMonth = salesOnThatMonth;
                        item1.SalesAmountOnThatMonth_RMB = salesAmountOnThatMonth_RMB;
                        item1.SalesAmountOnThatMonth_RUB = salesAmountOnThatMonth_RUB;
                        item1.ShipmentsOnThatMonth = shipmentsOnThatMonth;
                        item1.ShipmentsAmountOnThatMonth_RMB = shipmentsAmountOnThatMonth_RMB;
                        item1.ShipmentsAmountOnThatMonth_RUB = shipmentsAmountOnThatMonth_RUB;
                        item1.ReturnsOnThatMonth = returnsOnThatMonth;
                        item1.ReturnsAmountOnThatMonth_RMB = returnsAmountOnThatMonth_RMB;
                        item1.ReturnsAmountOnThatMonth_RUB = returnsAmountOnThatMonth_RUB;
                        item1.CancelOnThatMonth = cancelOnThatMonth;
                        item1.CancelAmountOnThatMonth_RMB = cancelAmountOnThatMonth_RMB;
                        item1.CancelAmountOnThatMonth_RUB = cancelAmountOnThatMonth_RUB;
                    }
                });

                //计算年的金额及数量
                dataList.GroupBy(x => x.Date.ToDateTime().ToString("yyyy") + x.MSKU + x.Seller_Id).ForEach(x =>
                {

                    var salesOnThatYear = x.Sum(x => x.SalesOnThatDay);
                    var salesAmountOnThatYear_RMB = x.Sum(x => x.SalesAmountOnThatDay_RMB);
                    var salesAmountOnThatYear_RUB = x.Sum(x => x.SalesAmountOnThatDay_RUB);
                    var shipmentsOnThatYear = x.Sum(x => x.ShipmentsOnThatDay);
                    var shipmentsAmountOnThatYear_RMB = x.Sum(x => x.ShipmentsAmountOnThatDay_RMB);
                    var shipmentsAmountOnThatYear_RUB = x.Sum(x => x.ShipmentsAmountOnThatDay_RUB);
                    var returnsOnThatYear = x.Sum(x => x.ReturnsOnThatDay);
                    var returnsAmountOnThatYear_RMB = x.Sum(x => x.ReturnsAmountOnThatDay_RMB);
                    var returnsAmountOnThatYear_RUB = x.Sum(x => x.ReturnsAmountOnThatDay_RUB);
                    var cancelOnThatYear = x.Sum(x => x.CancelOnThatDay);
                    var cancelAmountOnThatYear_RMB = x.Sum(x => x.CancelAmountOnThatDay_RMB);
                    var cancelAmountOnThatYear_RUB = x.Sum(x => x.CancelAmountOnThatDay_RUB);
                    foreach (var item1 in x)
                    {
                        item1.SalesOnThatYear = salesOnThatYear;
                        item1.SalesAmountOnThatYear_RMB = salesAmountOnThatYear_RMB;
                        item1.SalesAmountOnThatYear_RUB = salesAmountOnThatYear_RUB;
                        item1.ShipmentsOnThatYear = shipmentsOnThatYear;
                        item1.ShipmentsAmountOnThatYear_RMB = shipmentsAmountOnThatYear_RMB;
                        item1.ShipmentsAmountOnThatYear_RUB = shipmentsAmountOnThatYear_RUB;
                        item1.ReturnsOnThatYear = returnsOnThatYear;
                        item1.ReturnsAmountOnThatYear_RMB = returnsAmountOnThatYear_RMB;
                        item1.ReturnsAmountOnThatYear_RUB = returnsAmountOnThatYear_RUB;
                        item1.CancelOnThatYear = cancelOnThatYear;
                        item1.CancelAmountOnThatYear_RMB = cancelAmountOnThatYear_RMB;
                        item1.CancelAmountOnThatYear_RUB = cancelAmountOnThatYear_RUB;
                    }
                });

            }

            #endregion
            return dataList;
        }

        public List<OL_Rep_RussianEcommerceSales> GetWBSales(DateTime firstDate, List<View_Rate> rateList)
        {
            List<OL_Rep_RussianEcommerceSales> dataList = new List<OL_Rep_RussianEcommerceSales>();
            #region 查询数据源
            this.SqlDb._conStrings[0] = JsonSettingHelper.GetSetting("Database:BaseDb:ConnectionString");
            //获取年第一个月第一天的日期
            //var firstDateForYear = firstDate.Year + "-01-01";

            //发货报告明细
            List<WB_OrdersDTO.Orders> orderList_All = this.SqlDb.GetListBySql<WB_OrdersDTO.Orders>(@$"SELECT * FROM SD_WB_Orders WHERE CONVERT(date,[createdAt]) >= '{firstDate}'");
            //销售漏斗
            List<SaleStatistics> saleList_All = this.SqlDb.GetListBySql<SaleStatistics>(@$"SELECT * FROM SD_WB_SaleStatistics WHERE CONVERT(date,[begin])>='{firstDate}' AND   CONVERT(decimal(18,2),avgPriceRub) >0");
            //退货单
            List<BuyersReturn> returnList_All = this.SqlDb.GetListBySql<BuyersReturn>(@$"SELECT a.srid,a.nm_id,a.price,b.article imt_namee,CONVERT(DATE,dt) dt,b.Brand user_comment,b.PrdType wb_comment FROM SD_WB_BuyersReturn a LEFT JOIN 
SD_WB_Orders b ON a.srid=b.rid WHERE a.status='已归档' AND CONVERT(DATE,dt)>='{firstDate}'");
            //需要将退货没有订单的物料编号做关联(原退货单上没有产品编码)
            returnList_All.Where(x => x.imt_name.IsNullOrEmpty()).ForEach(x =>
            {
                var o = orderList_All.Where(y => y.nmId == x.nm_id);
                if (!o.IsNullOrEmpty())
                {
                    x.imt_name = o.First().article;
                    x.user_comment = o.First().Brand;
                    x.wb_comment = o.First().PrdType;
                }

            });

            List<string> selectList = new List<string>();
            //获取当日销量的产品，日期，店铺项
            var salesList = orderList_All.Where(x => !x.createdAt.IsNullOrEmpty() && x.createdAt.ToDateTime().Date >= firstDate).Select(x => x.createdAt.ToDateTime().ToString("yyyy-MM-dd") + "Wildberries" + x.article);
            var shipmentList = saleList_All.Where(x => !x.begin.IsNullOrEmpty() && x.begin.ToDateTime().Date >= firstDate).Select(x => x.begin.ToDateTime().ToString("yyyy-MM-dd") + "Wildberries" + x.vendorCode);
            var returnList = returnList_All.Where(x => !x.dt.IsNullOrEmpty() && x.dt.ToDateTime().Date >= firstDate).Select(x => x.dt.ToDateTime().ToString("yyyy-MM-dd") + "Wildberries" + x.imt_name);
            selectList.AddRange(salesList);
            selectList.AddRange(shipmentList);
            selectList.AddRange(returnList);
            var dis = selectList.Distinct().ToList();
            //根据产品，日期，店铺分组，统计日，月，年的销量,发货，退货以及金额信息
            foreach (var item1 in dis)
            {
                if (item1 == "2025-07-27WildberriesTIH59EB")
                {
                    var ww = "";

                }
                OL_Rep_RussianEcommerceSales data = new OL_Rep_RussianEcommerceSales();
                //string month = "";
                //string year = "";
                string date = "";
                string MSKU = "";
                string seller_Id = "";
                string prdType = "";
                string brand = "";

                //获取发货报告的销售，发货数据
                var salesTheDate = orderList_All.Where(x => (x.status != "declined_by_client" && x.status != "canceled" && x.supplierStatus != "cancel" && x.supplierStatus != "confirm" && x.supplierStatus != "new") && !x.createdAt.IsNullOrEmpty() && x.createdAt.ToDateTime().ToString("yyyy-MM-dd") + "Wildberries" + x.article == item1);
                var salesFirst = !salesTheDate.IsNullOrEmpty() && salesTheDate.Count() > 0 ? salesTheDate.First() : null;
                //获取发货报告的销售，取消数据
                var canceledTheDate = orderList_All.Where(x => (x.status_S == "买家取消" || x.status_S == "已取消") && !x.createdAt.IsNullOrEmpty() && x.createdAt.ToDateTime().ToString("yyyy-MM-dd") + "Wildberries" + x.article == item1);
                var canceledFirst = !canceledTheDate.IsNullOrEmpty() && canceledTheDate.Count() > 0 ? canceledTheDate.First() : null;
                //销售漏斗的销售，销量数据
                var salesStatisticsTheDate = saleList_All.Where(x => !x.begin.IsNullOrEmpty() && x.begin.ToDateTime().ToString("yyyy-MM-dd") + "Wildberries" + x.vendorCode == item1);
                var salesStatisticsFirst = !salesStatisticsTheDate.IsNullOrEmpty() && salesStatisticsTheDate.Count() > 0 ? salesStatisticsTheDate.First() : null;
                //退货订单，退货的数据
                var returnTheDate = returnList_All.Where(x => !x.dt.IsNullOrEmpty() && x.dt.ToDateTime().ToString("yyyy-MM-dd") + "Wildberries" + x.imt_name == item1);
                var returnTheFirst = !returnTheDate.IsNullOrEmpty() && returnTheDate.Count() > 0 ? returnTheDate.First() : null;
                //配置基础公共信息
                if (MSKU.IsNullOrEmpty() && !salesFirst.IsNullOrEmpty())
                {
                    MSKU = salesFirst.article;
                    seller_Id = "Wildberries";
                    prdType = salesFirst.PrdType;
                    brand = salesFirst.Brand;
                    date = salesFirst.createdAt.ToDateTime().ToString("yyyy-MM-dd");
                }
                else if (MSKU.IsNullOrEmpty() && !canceledFirst.IsNullOrEmpty())
                {
                    MSKU = canceledFirst.article;
                    seller_Id = "Wildberries";
                    prdType = canceledFirst.PrdType;
                    brand = canceledFirst.Brand;
                    date = canceledFirst.createdAt.ToDateTime().ToString("yyyy-MM-dd");

                }
                else if (MSKU.IsNullOrEmpty() && !salesStatisticsFirst.IsNullOrEmpty())
                {
                    MSKU = salesStatisticsFirst.vendorCode;
                    seller_Id = "Wildberries";
                    prdType = salesStatisticsFirst.PrdType;
                    brand = salesStatisticsFirst.Brand;
                    date = salesStatisticsFirst.begin.ToDateTime().ToString("yyyy-MM-dd");

                }
                else
                {
                    continue;

                }

                //日销量
                var sales = salesStatisticsTheDate;//将取消的数量去除算销量
                data.SalesOnThatDay = !sales.IsNullOrEmpty() && sales.Count() > 0 ? sales.Sum(x => (decimal)x.ordersCount.ToDouble()) : 0;
                //日销售额RUM
                data.SalesAmountOnThatDay_RUB = !sales.IsNullOrEmpty() && sales.Count() > 0 ? (decimal)sales.First().avgPriceRub.ToDouble() * data.SalesOnThatDay : 0;



                //日退货
                data.ReturnsOnThatDay = !returnTheDate.IsNullOrEmpty() && returnTheDate.Count() > 0 ? returnTheDate.Count() : 0;
                //日退货额RUM
                data.ReturnsAmountOnThatDay_RUB = !returnTheDate.IsNullOrEmpty() && returnTheDate.Count() > 0 ? (decimal)returnTheDate.Sum(x => x.price.ToDouble()) : 0;



                //日发货
                var shipments = salesTheDate;//将取消的数量去除算销量
                data.ShipmentsOnThatDay = !shipments.IsNullOrEmpty() && shipments.Count() > 0 ? shipments.Count() : 0;
                //日发货额RUM
                data.ShipmentsAmountOnThatDay_RUB = !shipments.IsNullOrEmpty() && shipments.Count() > 0 ? (decimal)shipments.Sum(x => x.convertedPrice.ToDouble()) : 0;

                //日取消
                var canceleds = canceledTheDate;//将取消的数量去除算销量
                data.CancelOnThatDay = !canceleds.IsNullOrEmpty() && canceleds.Count() > 0 ? canceleds.Count() : 0;
                //日取消额RUM
                data.CancelAmountOnThatDay_RUB = !canceleds.IsNullOrEmpty() && canceleds.Count() > 0 ? (decimal)canceleds.Sum(x => x.convertedPrice.ToDouble()) : 0;





                if (!date.IsNullOrEmpty() && !MSKU.IsNullOrEmpty())
                {
                    //获取汇率
                    var rate_ = rateList.Where(x => x.生效时间.ToDateTime().Date == date.ToDateTime().Date);
                    var rate = rate_.IsNullOrEmpty() ? 0 : rate_.First().汇率.ToDouble();
                    //日销售额RMB
                    data.SalesAmountOnThatDay_RMB = rate == 0 || data.SalesAmountOnThatDay_RUB == 0 ? 0 : Math.Round(data.SalesAmountOnThatDay_RUB / (decimal)rate, 2);
                    //日发货额RMB
                    data.ShipmentsAmountOnThatDay_RMB = rate == 0 || data.ShipmentsAmountOnThatDay_RUB == 0 ? 0 : Math.Round(data.ShipmentsAmountOnThatDay_RUB / (decimal)rate, 2);
                    //日退货额RMB
                    data.ReturnsAmountOnThatDay_RMB = rate == 0 || data.ReturnsAmountOnThatDay_RUB == 0 ? 0 : Math.Round(data.ReturnsAmountOnThatDay_RUB / (decimal)rate, 2);
                    //日取消额RMB
                    data.CancelAmountOnThatDay_RMB = rate == 0 || data.CancelAmountOnThatDay_RUB == 0 ? 0 : Math.Round(data.CancelAmountOnThatDay_RUB / (decimal)rate, 2);
                    data.Date = date;
                    data.MSKU = MSKU;
                    data.PrdType = prdType;
                    data.Brand = brand;
                    data.Seller_Id = seller_Id;
                    data.ID = data.MSKU + data.Date + data.Seller_Id;
                    dataList.Add(data);
                }
            }
            if (!dataList.IsNullOrEmpty() && dataList.Count > 0)
            {
                //计算日的金额及数量
                //dataList.GroupBy(x => x.Date.ToDateTime().ToString("yyyy-MM-dd") + x.MSKU + x.Seller_Id).ForEach(x =>
                //{

                //    var salesOnThatDay = x.Sum(x => x.SalesOnThatDay);
                //    var salesAmountOnThatDay_RMB = x.Sum(x => x.SalesAmountOnThatDay_RMB);
                //    var salesAmountOnThatDay_RUB = x.Sum(x => x.SalesAmountOnThatDay_RUB);
                //    var shipmentsOnThatDay = x.Sum(x => x.ShipmentsOnThatDay);
                //    var shipmentsAmountOnThatDay_RMB = x.Sum(x => x.ShipmentsAmountOnThatDay_RMB);
                //    var shipmentsAmountOnThatDay_RUB = x.Sum(x => x.ShipmentsAmountOnThatDay_RUB);
                //    var returnsOnThatDay = x.Sum(x => x.ReturnsOnThatDay);
                //    var returnsAmountOnThatDay_RMB = x.Sum(x => x.ReturnsAmountOnThatDay_RMB);
                //    var returnsAmountOnThatDay_RUB = x.Sum(x => x.ReturnsAmountOnThatDay_RUB);

                //    foreach (var item1 in x)
                //    {
                //        item1.SalesOnThatDay = salesOnThatDay;
                //        item1.SalesAmountOnThatDay_RMB = salesAmountOnThatDay_RMB;
                //        item1.SalesAmountOnThatDay_RUB = salesAmountOnThatDay_RUB;
                //        item1.ShipmentsOnThatDay = shipmentsOnThatDay;
                //        item1.ShipmentsAmountOnThatDay_RMB = shipmentsAmountOnThatDay_RMB;
                //        item1.ShipmentsAmountOnThatDay_RUB = shipmentsAmountOnThatDay_RUB;
                //        item1.ReturnsOnThatDay = returnsOnThatDay;
                //        item1.ReturnsAmountOnThatDay_RMB = returnsAmountOnThatDay_RMB;
                //        item1.ReturnsAmountOnThatDay_RUB = returnsAmountOnThatDay_RUB;
                //    }
                //});
                //计算月的金额及数量
                dataList.GroupBy(x => x.Date.ToDateTime().ToString("yyyy-MM") + x.MSKU + x.Seller_Id).ForEach(x =>
                {

                    var salesOnThatMonth = x.Sum(x => x.SalesOnThatDay);
                    var salesAmountOnThatMonth_RMB = x.Sum(x => x.SalesAmountOnThatDay_RMB);
                    var salesAmountOnThatMonth_RUB = x.Sum(x => x.SalesAmountOnThatDay_RUB);
                    var shipmentsOnThatMonth = x.Sum(x => x.ShipmentsOnThatDay);
                    var shipmentsAmountOnThatMonth_RMB = x.Sum(x => x.ShipmentsAmountOnThatDay_RMB);
                    var shipmentsAmountOnThatMonth_RUB = x.Sum(x => x.ShipmentsAmountOnThatDay_RUB);
                    var returnsOnThatMonth = x.Sum(x => x.ReturnsOnThatDay);
                    var returnsAmountOnThatMonth_RMB = x.Sum(x => x.ReturnsAmountOnThatDay_RMB);
                    var returnsAmountOnThatMonth_RUB = x.Sum(x => x.ReturnsAmountOnThatDay_RUB);
                    var cancelOnThatMonth = x.Sum(x => x.CancelOnThatDay);
                    var cancelAmountOnThatMonth_RMB = x.Sum(x => x.CancelAmountOnThatDay_RMB);
                    var cancelAmountOnThatMonth_RUB = x.Sum(x => x.CancelAmountOnThatDay_RUB);
                    foreach (var item1 in x)
                    {
                        item1.SalesOnThatMonth = salesOnThatMonth;
                        item1.SalesAmountOnThatMonth_RMB = salesAmountOnThatMonth_RMB;
                        item1.SalesAmountOnThatMonth_RUB = salesAmountOnThatMonth_RUB;
                        item1.ShipmentsOnThatMonth = shipmentsOnThatMonth;
                        item1.ShipmentsAmountOnThatMonth_RMB = shipmentsAmountOnThatMonth_RMB;
                        item1.ShipmentsAmountOnThatMonth_RUB = shipmentsAmountOnThatMonth_RUB;
                        item1.ReturnsOnThatMonth = returnsOnThatMonth;
                        item1.ReturnsAmountOnThatMonth_RMB = returnsAmountOnThatMonth_RMB;
                        item1.ReturnsAmountOnThatMonth_RUB = returnsAmountOnThatMonth_RUB;
                        item1.CancelOnThatMonth = cancelOnThatMonth;
                        item1.CancelAmountOnThatMonth_RMB = cancelAmountOnThatMonth_RMB;
                        item1.CancelAmountOnThatMonth_RUB = cancelAmountOnThatMonth_RUB;
                    }
                });

                //计算年的金额及数量
                dataList.GroupBy(x => x.Date.ToDateTime().ToString("yyyy") + x.MSKU + x.Seller_Id).ForEach(x =>
                {

                    var salesOnThatYear = x.Sum(x => x.SalesOnThatDay);
                    var salesAmountOnThatYear_RMB = x.Sum(x => x.SalesAmountOnThatDay_RMB);
                    var salesAmountOnThatYear_RUB = x.Sum(x => x.SalesAmountOnThatDay_RUB);
                    var shipmentsOnThatYear = x.Sum(x => x.ShipmentsOnThatDay);
                    var shipmentsAmountOnThatYear_RMB = x.Sum(x => x.ShipmentsAmountOnThatDay_RMB);
                    var shipmentsAmountOnThatYear_RUB = x.Sum(x => x.ShipmentsAmountOnThatDay_RUB);
                    var returnsOnThatYear = x.Sum(x => x.ReturnsOnThatDay);
                    var returnsAmountOnThatYear_RMB = x.Sum(x => x.ReturnsAmountOnThatDay_RMB);
                    var returnsAmountOnThatYear_RUB = x.Sum(x => x.ReturnsAmountOnThatDay_RUB);
                    var cancelOnThatYear = x.Sum(x => x.CancelOnThatDay);
                    var cancelAmountOnThatYear_RMB = x.Sum(x => x.CancelAmountOnThatDay_RMB);
                    var cancelAmountOnThatYear_RUB = x.Sum(x => x.CancelAmountOnThatDay_RUB);
                    foreach (var item1 in x)
                    {
                        item1.SalesOnThatYear = salesOnThatYear;
                        item1.SalesAmountOnThatYear_RMB = salesAmountOnThatYear_RMB;
                        item1.SalesAmountOnThatYear_RUB = salesAmountOnThatYear_RUB;
                        item1.ShipmentsOnThatYear = shipmentsOnThatYear;
                        item1.ShipmentsAmountOnThatYear_RMB = shipmentsAmountOnThatYear_RMB;
                        item1.ShipmentsAmountOnThatYear_RUB = shipmentsAmountOnThatYear_RUB;
                        item1.ReturnsOnThatYear = returnsOnThatYear;
                        item1.ReturnsAmountOnThatYear_RMB = returnsAmountOnThatYear_RMB;
                        item1.ReturnsAmountOnThatYear_RUB = returnsAmountOnThatYear_RUB;
                        item1.CancelOnThatYear = cancelOnThatYear;
                        item1.CancelAmountOnThatYear_RMB = cancelAmountOnThatYear_RMB;
                        item1.CancelAmountOnThatYear_RUB = cancelAmountOnThatYear_RUB;
                    }
                });


            }

            #endregion
            return dataList;
        }


        public List<OL_Rep_RussianEcommerceSales> GetYDSales(DateTime firstDate, List<View_Rate> rateList)
        {
            List<OL_Rep_RussianEcommerceSales> dataList = new List<OL_Rep_RussianEcommerceSales>();
            #region 查询数据源
            this.SqlDb._conStrings[0] = JsonSettingHelper.GetSetting("Database:BaseDb:ConnectionString");
            //订单明细
            List<YDOrdersDTO.Orders> orderList_All = this.SqlDb.GetListBySql<YDOrdersDTO.Orders>(@$"SELECT * FROM SD_YD_Orders WHERE CONVERT(date,[creationDate])>='{firstDate}'");
            //退货明细
            List<YDReturnsDTO.Returns> returnsList_All = this.SqlDb.GetListBySql<YDReturnsDTO.Returns>(@$"SELECT a.items_shopSku,a.SellerId,a.orderId,a.creationDate,a.amount_value,a.items_count,b.Brand logisticPickupPoint_name,B.Type logisticPickupPoint_instruction FROM SD_YD_Returns a
LEFT JOIN SD_YD_Orders b ON a.orderId=b.id
WHERE b.status!='已取消' AND  CONVERT(date,a.creationDate)>='{firstDate}'");
            List<string> selectList = new List<string>();
            //获取当日销量的产品，日期，店铺项,订单包含了订购，更新(退货需要用)，发货日期，需要将所有的日期一起纳入循环
            var salesList = orderList_All.Where(x => !x.creationDate.IsNullOrEmpty()).Select(x => x.creationDate.ToDateTime().ToString("yyyy-MM-dd") + x.SellerId + x.offerId);
            var returnList = returnsList_All.Where(x => !x.creationDate.IsNullOrEmpty()).Select(x => x.creationDate.ToDateTime().ToString("yyyy-MM-dd") + x.SellerId + x.items_shopSku);
            selectList.AddRange(salesList);
            selectList.AddRange(returnList);


            //var updateList = orderList_All.Where(x => !x.updatedAt.IsNullOrEmpty()).Select(x => x.updatedAt.ToDateTime().ToString("yyyy-MM-dd") + "Yandex" + x.offerId);
            //selectList.AddRange(updateList);
            //var shipmentList = orderList_All.Where(x => !x.shipments_shipmentDate.IsNullOrEmpty()).Select(x => x.shipments_shipmentDate.ToDateTime().ToString("yyyy-MM-dd") + "Yandex" + x.offerId);
            //selectList.AddRange(shipmentList);



            //根据产品，日期，店铺分组，统计日，月，年的销量,发货，退货以及金额信息
            foreach (var item1 in selectList.Distinct())
            {
                try
                {

                    if (item1 == "2025-11-18YandexTCH59BB")
                    {
                        var ww = "";

                    }
                    OL_Rep_RussianEcommerceSales data = new OL_Rep_RussianEcommerceSales();
                    //string month = "";
                    //string year = "";
                    string date = "";
                    string MSKU = "";
                    string seller_Id = "";
                    string prdType = "";
                    string brand = "";

                    //获取产品当日的销售数量，发货数量，退货数量，店铺相同,产品编号相同，时间相同
                    var salesTheDate = orderList_All.Where(x => !x.creationDate.IsNullOrEmpty() && x.creationDate.ToDateTime().ToString("yyyy-MM-dd") + x.SellerId + x.offerId == item1);
                    var salesFirst = !salesTheDate.IsNullOrEmpty() && salesTheDate.Count() > 0 ? salesTheDate.First() : null;
                    //日取消数据
                    var canceledTheDate = !salesTheDate.IsNullOrEmpty() && salesTheDate.Count() > 0 ? salesTheDate.Where(x => x.status == "已取消") : null;
                    var canceledFirst = !canceledTheDate.IsNullOrEmpty() && canceledTheDate.Count() > 0 ? canceledTheDate.First() : null;
                    //日发货数据
                    var shipmentsTheDate = !salesTheDate.IsNullOrEmpty() && salesTheDate.Count() > 0 ? salesTheDate.Where(x => !x.shipments_shipmentDate.IsNullOrEmpty() && (x.status == "配送中" || (x.shipments_shipmentDate.ToDateTime() < DateTime.Now.Date && x.status != "处理中"))) : null;
                    //当日的退货项
                    var returnTheDate = !returnsList_All.IsNullOrEmpty() && returnsList_All.Count() > 0 ? returnsList_All.Where(x => !x.creationDate.IsNullOrEmpty() && x.creationDate.ToDateTime().ToString("yyyy-MM-dd") + x.SellerId + x.items_shopSku == item1) : null;
                    var returnFirst = !returnTheDate.IsNullOrEmpty() && returnTheDate.Count() > 0 ? returnTheDate.First() : null;

                    //配置基本公共信息
                    if (MSKU.IsNullOrEmpty() && !salesFirst.IsNullOrEmpty())
                    {
                        MSKU = salesFirst.offerId;
                        seller_Id = salesFirst.SellerId;
                        prdType = salesFirst.Type;
                        brand = salesFirst.Brand;
                        date = salesFirst.creationDate.ToDateTime().ToString("yyyy-MM-dd");
                    }
                    else if (MSKU.IsNullOrEmpty() && !returnFirst.IsNullOrEmpty())
                    {
                        MSKU = returnFirst.items_shopSku;
                        seller_Id = returnFirst.SellerId;
                        prdType = returnFirst.logisticPickupPoint_instruction;
                        brand = returnFirst.logisticPickupPoint_name;
                        date = returnFirst.creationDate.ToDateTime().ToString("yyyy-MM-dd");
                    }
                    //日销量
                    data.SalesOnThatDay = !salesTheDate.IsNullOrEmpty() && salesTheDate.Count() > 0 ? salesTheDate.Sum(x => x.count.ToInt()) : 0;
                    //日销售额RUM
                    data.SalesAmountOnThatDay_RUB = !salesTheDate.IsNullOrEmpty() && salesTheDate.Count() > 0 ? (decimal)salesTheDate.Sum(x => (x.buyerPrice.ToDouble() + x.promosSubsidy.ToDouble()) * x.count.ToInt()) : 0;


                    //日发货
                    if (!shipmentsTheDate.IsNullOrEmpty())
                    {
                        //日发货
                        data.ShipmentsOnThatDay = !shipmentsTheDate.IsNullOrEmpty() && shipmentsTheDate.Count() > 0 ? shipmentsTheDate.Sum(x => x.count.ToInt()) : 0;
                        //日发货额RUM
                        data.ShipmentsAmountOnThatDay_RUB = !shipmentsTheDate.IsNullOrEmpty() && shipmentsTheDate.Count() > 0 ? (decimal)shipmentsTheDate.Sum(x => (x.buyerPrice.ToDouble() + x.promosSubsidy.ToDouble()) * x.count.ToInt()) : 0;

                    }
                    else
                    {
                        data.ShipmentsOnThatDay = 0;
                        data.ShipmentsAmountOnThatDay_RUB = 0;
                    }

                    //日取消
                    if (!canceledTheDate.IsNullOrEmpty())
                    {
                        //日取消
                        data.CancelOnThatDay = !canceledTheDate.IsNullOrEmpty() && canceledTheDate.Count() > 0 ? canceledTheDate.Sum(x => x.count.ToInt()) : 0;
                        //日取消额RUM
                        data.CancelAmountOnThatDay_RUB = !canceledTheDate.IsNullOrEmpty() && canceledTheDate.Count() > 0 ? (decimal)canceledTheDate.Sum(x => (x.buyerPrice.ToDouble() + x.promosSubsidy.ToDouble()) * x.count.ToInt()) : 0;

                    }
                    else
                    {
                        data.CancelOnThatDay = 0;
                        data.CancelAmountOnThatDay_RUB = 0;
                    }



                    //当日的退货项
                    if (!returnTheDate.IsNullOrEmpty())
                    {
                        //日退货
                        data.ReturnsOnThatDay = !returnTheDate.IsNullOrEmpty() && returnTheDate.Count() > 0 ? returnTheDate.Sum(x => x.items_count.ToInt()) : 0;
                        //日退货额RUM
                        data.ReturnsAmountOnThatDay_RUB = !returnTheDate.IsNullOrEmpty() && returnTheDate.Count() > 0 ? (decimal)returnTheDate.Sum(x => x.amount_value.ToDouble()) : 0;

                    }
                    else
                    {
                        data.ReturnsOnThatDay = 0;
                        data.ReturnsAmountOnThatDay_RUB = 0;
                    }




                    if (!date.IsNullOrEmpty() && !MSKU.IsNullOrEmpty())
                    {
                        //获取汇率
                        var rate_ = rateList.Where(x => x.生效时间.ToDateTime().Date == date.ToDateTime().Date);
                        var rate = rate_.IsNullOrEmpty() ? 0 : rate_.First().汇率.ToDouble();
                        //日销售额RMB
                        data.SalesAmountOnThatDay_RMB = rate == 0 || data.SalesAmountOnThatDay_RUB == 0 ? 0 : Math.Round(data.SalesAmountOnThatDay_RUB / (decimal)rate, 2);
                        //日发货额RMB
                        data.ShipmentsAmountOnThatDay_RMB = rate == 0 || data.ShipmentsAmountOnThatDay_RUB == 0 ? 0 : Math.Round(data.ShipmentsAmountOnThatDay_RUB / (decimal)rate, 2);
                        //日退货额RMB
                        data.ReturnsAmountOnThatDay_RMB = rate == 0 || data.ReturnsAmountOnThatDay_RUB == 0 ? 0 : Math.Round(data.ReturnsAmountOnThatDay_RUB / (decimal)rate, 2);
                        //日取消额RMB
                        data.CancelAmountOnThatDay_RMB = rate == 0 || data.CancelAmountOnThatDay_RUB == 0 ? 0 : Math.Round(data.CancelAmountOnThatDay_RUB / (decimal)rate, 2);
                        data.Date = date;
                        data.MSKU = MSKU;
                        data.PrdType = prdType;
                        data.Brand = brand;
                        data.Seller_Id = seller_Id;
                        data.ID = data.MSKU + data.Date + data.Seller_Id;
                        dataList.Add(data);
                    }

                }
                catch (Exception ex)
                {
                    AddLog(SqlDb, ex.Message, InterfaceName, "erro", "");
                }
            }




            if (!dataList.IsNullOrEmpty() && dataList.Count > 0)
            {
                //计算日的金额及数量
                //dataList.GroupBy(x => x.Date.ToDateTime().ToString("yyyy-MM-dd") + x.MSKU + x.Seller_Id).ForEach(x =>
                //{

                //    var salesOnThatDay = x.Sum(x => x.SalesOnThatDay);
                //    var salesAmountOnThatDay_RMB = x.Sum(x => x.SalesAmountOnThatDay_RMB);
                //    var salesAmountOnThatDay_RUB = x.Sum(x => x.SalesAmountOnThatDay_RUB);
                //    var shipmentsOnThatDay = x.Sum(x => x.ShipmentsOnThatDay);
                //    var shipmentsAmountOnThatDay_RMB = x.Sum(x => x.ShipmentsAmountOnThatDay_RMB);
                //    var shipmentsAmountOnThatDay_RUB = x.Sum(x => x.ShipmentsAmountOnThatDay_RUB);
                //    var returnsOnThatDay = x.Sum(x => x.ReturnsOnThatDay);
                //    var returnsAmountOnThatDay_RMB = x.Sum(x => x.ReturnsAmountOnThatDay_RMB);
                //    var returnsAmountOnThatDay_RUB = x.Sum(x => x.ReturnsAmountOnThatDay_RUB);

                //    foreach (var item1 in x)
                //    {
                //        item1.SalesOnThatDay = salesOnThatDay;
                //        item1.SalesAmountOnThatDay_RMB = salesAmountOnThatDay_RMB;
                //        item1.SalesAmountOnThatDay_RUB = salesAmountOnThatDay_RUB;
                //        item1.ShipmentsOnThatDay = shipmentsOnThatDay;
                //        item1.ShipmentsAmountOnThatDay_RMB = shipmentsAmountOnThatDay_RMB;
                //        item1.ShipmentsAmountOnThatDay_RUB = shipmentsAmountOnThatDay_RUB;
                //        item1.ReturnsOnThatDay = returnsOnThatDay;
                //        item1.ReturnsAmountOnThatDay_RMB = returnsAmountOnThatDay_RMB;
                //        item1.ReturnsAmountOnThatDay_RUB = returnsAmountOnThatDay_RUB;
                //    }
                //});

                //计算月的金额及数量
                dataList.GroupBy(x => x.Date.ToDateTime().ToString("yyyy-MM") + x.MSKU + x.Seller_Id).ForEach(x =>
                {

                    var salesOnThatMonth = x.Sum(x => x.SalesOnThatDay);
                    var salesAmountOnThatMonth_RMB = x.Sum(x => x.SalesAmountOnThatDay_RMB);
                    var salesAmountOnThatMonth_RUB = x.Sum(x => x.SalesAmountOnThatDay_RUB);
                    var shipmentsOnThatMonth = x.Sum(x => x.ShipmentsOnThatDay);
                    var shipmentsAmountOnThatMonth_RMB = x.Sum(x => x.ShipmentsAmountOnThatDay_RMB);
                    var shipmentsAmountOnThatMonth_RUB = x.Sum(x => x.ShipmentsAmountOnThatDay_RUB);
                    var returnsOnThatMonth = x.Sum(x => x.ReturnsOnThatDay);
                    var returnsAmountOnThatMonth_RMB = x.Sum(x => x.ReturnsAmountOnThatDay_RMB);
                    var returnsAmountOnThatMonth_RUB = x.Sum(x => x.ReturnsAmountOnThatDay_RUB);
                    var cancelOnThatMonth = x.Sum(x => x.CancelOnThatDay);
                    var cancelAmountOnThatMonth_RMB = x.Sum(x => x.CancelAmountOnThatDay_RMB);
                    var cancelAmountOnThatMonth_RUB = x.Sum(x => x.CancelAmountOnThatDay_RUB);
                    foreach (var item1 in x)
                    {
                        item1.SalesOnThatMonth = salesOnThatMonth;
                        item1.SalesAmountOnThatMonth_RMB = salesAmountOnThatMonth_RMB;
                        item1.SalesAmountOnThatMonth_RUB = salesAmountOnThatMonth_RUB;
                        item1.ShipmentsOnThatMonth = shipmentsOnThatMonth;
                        item1.ShipmentsAmountOnThatMonth_RMB = shipmentsAmountOnThatMonth_RMB;
                        item1.ShipmentsAmountOnThatMonth_RUB = shipmentsAmountOnThatMonth_RUB;
                        item1.ReturnsOnThatMonth = returnsOnThatMonth;
                        item1.ReturnsAmountOnThatMonth_RMB = returnsAmountOnThatMonth_RMB;
                        item1.ReturnsAmountOnThatMonth_RUB = returnsAmountOnThatMonth_RUB;
                        item1.CancelOnThatMonth = cancelOnThatMonth;
                        item1.CancelAmountOnThatMonth_RMB = cancelAmountOnThatMonth_RMB;
                        item1.CancelAmountOnThatMonth_RUB = cancelAmountOnThatMonth_RUB;
                    }
                });

                //计算年的金额及数量
                dataList.GroupBy(x => x.Date.ToDateTime().ToString("yyyy") + x.MSKU + x.Seller_Id).ForEach(x =>
                {

                    var salesOnThatYear = x.Sum(x => x.SalesOnThatDay);
                    var salesAmountOnThatYear_RMB = x.Sum(x => x.SalesAmountOnThatDay_RMB);
                    var salesAmountOnThatYear_RUB = x.Sum(x => x.SalesAmountOnThatDay_RUB);
                    var shipmentsOnThatYear = x.Sum(x => x.ShipmentsOnThatDay);
                    var shipmentsAmountOnThatYear_RMB = x.Sum(x => x.ShipmentsAmountOnThatDay_RMB);
                    var shipmentsAmountOnThatYear_RUB = x.Sum(x => x.ShipmentsAmountOnThatDay_RUB);
                    var returnsOnThatYear = x.Sum(x => x.ReturnsOnThatDay);
                    var returnsAmountOnThatYear_RMB = x.Sum(x => x.ReturnsAmountOnThatDay_RMB);
                    var returnsAmountOnThatYear_RUB = x.Sum(x => x.ReturnsAmountOnThatDay_RUB);
                    var cancelOnThatYear = x.Sum(x => x.CancelOnThatDay);
                    var cancelAmountOnThatYear_RMB = x.Sum(x => x.CancelAmountOnThatDay_RMB);
                    var cancelAmountOnThatYear_RUB = x.Sum(x => x.CancelAmountOnThatDay_RUB);
                    foreach (var item1 in x)
                    {
                        item1.SalesOnThatYear = salesOnThatYear;
                        item1.SalesAmountOnThatYear_RMB = salesAmountOnThatYear_RMB;
                        item1.SalesAmountOnThatYear_RUB = salesAmountOnThatYear_RUB;
                        item1.ShipmentsOnThatYear = shipmentsOnThatYear;
                        item1.ShipmentsAmountOnThatYear_RMB = shipmentsAmountOnThatYear_RMB;
                        item1.ShipmentsAmountOnThatYear_RUB = shipmentsAmountOnThatYear_RUB;
                        item1.ReturnsOnThatYear = returnsOnThatYear;
                        item1.ReturnsAmountOnThatYear_RMB = returnsAmountOnThatYear_RMB;
                        item1.ReturnsAmountOnThatYear_RUB = returnsAmountOnThatYear_RUB;
                        item1.CancelOnThatYear = cancelOnThatYear;
                        item1.CancelAmountOnThatYear_RMB = cancelAmountOnThatYear_RMB;
                        item1.CancelAmountOnThatYear_RUB = cancelAmountOnThatYear_RUB;
                    }
                });

            }

            #endregion
            return dataList;
        }


        /// <summary>
        /// 任务
        /// </summary>
        public void Job()
        {
            ExecuteInterface();
        }
    }

}
